package com.lh0811.forge.pserv.uacs.server.component.data_scope.annotation;


import com.lh0811.forge.pserv.uacs.api.component.data_scope.DataScope;
import com.lh0811.forge.pserv.uacs.server.component.data_scope.annotation.handler.DataScopeThreadLocalHandler;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;


@Component
@Aspect
@Slf4j
public class DataScopeAop {


    @Pointcut("@within(com.lh0811.forge.pserv.uacs.api.component.data_scope.DataScope) || @annotation(com.lh0811.forge.pserv.uacs.api.component.data_scope.DataScope)")
    public void annotationPointCut() {
    }


    @Around("annotationPointCut()")
    public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
        log.info("进入DataScopeAop切面");
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        DataScope annotation = signature.getMethod().getAnnotation(DataScope.class);
        // 保存注解
        DataScopeThreadLocalHandler.setDataScope(annotation);
        // 继续执行方法
        Object proceed = joinPoint.proceed(joinPoint.getArgs());
        // 移除注解
        DataScopeThreadLocalHandler.cleanDataScope();
        log.info("退出DataScopeAop切面");
        return proceed;
    }

}
